package gameStates.gui;

import java.util.concurrent.Callable;

import com.jme.util.GameTaskQueueManager;

import gameStates.GameTimeListener;
import gameStates.passes.AbsPassManagerState;
import interfaces.hud.end.EndWindow;
import logic.common.game.Game;
import logic.common.team.Team;
import main.AbsInitGame;
import main.InitGame;
import main.listeners.listenerClasses.gui.ChatActivatedEvent;
import main.listeners.listenerClasses.gui.ConsoleToggleEvent;
import main.listeners.listenerClasses.gui.GUIListener;
import main.listeners.listenerClasses.gui.IngameChangedEvent;
import main.listeners.listenerClasses.gui.ScoreBoardToggleEvent;
import main.listeners.listenerClasses.gui.SpawnMenuChangedEvent;

/**
 * This {@link GUIState} will be displayed if the current {@link Game} ends. It basically just shows 
 * an {@link EndWindow}.
 * 
 * @author Wasserleiche
 */
public class EndWindowState extends GUIState {
		
	/** The {@link EndWindow} that shows all information about the end of the {@link Game}. */
	private EndWindow endWindow;
	/** The {@link Game} that just ended. */
	private Game game;
	/** The {@link Team} that won the {@link Game}. */
	private Team winner;
	
	/**
	 * Creates a new EndWindowState with the given information.
	 * @param g The {@link Game} that ended.
	 * @param winner The winner-{@link Team}.
	 */
	public EndWindowState(Game g, Team winner, AbsPassManagerState passManagerState) {
		super("end", passManagerState, true);
		this.game = g;
		this.winner = winner;
		
		addListenerToClearer(new GUIListener() {
			@Override
			public void ingameChanged(IngameChangedEvent event) {
				setActive(event.isIngame());
			}
			
			@Override
			public void consoleToggled(ConsoleToggleEvent event) {}
			@Override
			public void spawnMenuChanged(SpawnMenuChangedEvent event) {}
			@Override
			public void scoreBoardToggled(ScoreBoardToggleEvent event) {}
			@Override
			public void chatActivated(ChatActivatedEvent event) {}
		});
		
		winner.getIngameState().addGameTimeListener(new GameTimeListener() {
			@Override
			public void respawnTimeUpdate() {}
			@Override
			public void gameTimeUpdate(Integer newGameTime) {}
			@Override
			public void endTimeUpdate(Integer newEndTime) {
				endWindow.updateCountDown(newEndTime.intValue());
				if(newEndTime.intValue() <= 0) {
					GameTaskQueueManager.getManager().update(new Callable<Void>() {
						@Override
						public Void call() throws Exception {
							AbsInitGame init = InitGame.get();
							init.endCurrentGame();
							init.initNewGame(init.isServer(), game.getGameCycle());
							return null;
						}
					});
				}
			}
		});
		
		initInterface();
		
		putAtSecondEnd();
	}
	
	/**
	 * Creates a new {@link EndWindow} with the current {@link Game} and winner-{@link Team} and attaches 
	 * it to the {@link Display}. It will also be stored in the windows-list.
	 */
	public void initInterface() {
		endWindow = new EndWindow(game, winner, this);
		endWindow.addToContainer(guiDisp);
		guiDisp.layout();
	}
	
	/**
	 * Returns the current {@link EndWindow}.
	 * @return The {@link EndWindow}.
	 */
	public EndWindow getEndWindow() { return endWindow; }
	
	/**
	 * Recreates the {@link EndWindow}.
	 */
	@Override
	public void restore() {
		super.restore();
		initInterface();
	}
}